package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.dao.PermissionDao;
import com.msun.dao.RoleDao;
import com.msun.dao.UserDao;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.pojo.*;
import com.msun.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

//    private PasswordEncoder passwordEncoder;
//@Autowired
//    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public List<Map<String,String>> findHealthManagerList() {
        return userDao.findHealthManager();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        // 执行分页查询获取到套餐
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.findPage(queryString);
        //获取用户对应的角色
        for (User user : page) {
            user.setPassword("想看吗?");
            Set<Role> byUserId = roleDao.findByUserId(user.getId());
            user.setRoles(byUserId);
        }
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public void add(Map<String, Object> map) {
        Object userOBJ =  map.get("user");
        // 权限id
        Object rolesOBJ =map.get("roleIds");
        String userSTR = JSONObject.toJSONString(userOBJ);
        User user  = JSONObject.parseObject(userSTR, User.class);
        // 给密码加密
        BCryptPasswordEncoder encoder =new BCryptPasswordEncoder();
        user.setPassword( encoder.encode(user.getPassword()));
        String rolesSTR = JSONObject.toJSONString(rolesOBJ);
        List<Integer> rols = JSONObject.parseObject(rolesSTR, List.class);
        // 新增user后把id拿回来
        userDao.add(user);
        for (Integer itemId : rols) {
            // 添加中间表的关系
            roleDao.addRefer(user.getId(),itemId);
        }
    }
    @Override
    public void edit(Map<String,Object> map) {
        Object userOBJ =  map.get("user");
        Object rolesOBJ =map.get("roleIds");
        String userSTR = JSONObject.toJSONString(userOBJ);
        User user  = JSONObject.parseObject(userSTR, User.class);
        // 给密码加密
        BCryptPasswordEncoder encoder =new BCryptPasswordEncoder();
        user.setPassword( encoder.encode(user.getPassword()));
        String rolesSTR = JSONObject.toJSONString(rolesOBJ);
        List<Integer> rols = JSONObject.parseObject(rolesSTR, List.class);
        // 先保存主体
        userDao.edit(user);
        Integer userId = user.getId();
        // 先删除和该user相关的关系
        roleDao.deleteRefer(userId);
        // 重新添加关系
        for (Integer itemId : rols) {
            roleDao.addRefer(userId,itemId);
        }
    }
    @Override
    public void delete(Integer id) {
        // 先删用户关联的权限
        roleDao.deleteRefer(id);
        // 再删主体
        userDao.delete(id);

    }
    @Override
    public User findByUsername2(String username) {
        User user = userDao.findByUserName2(username);
        if(user==null){
            return null;
        }
        // 设置用户的角色
        Set<Role> roles = roleDao.findByUserId2(user.getId());
        user.setRoles(roles);
        // 遍历roles,设置角色对应的权限
        for (Role role : roles) {
            Set<Permission> permissions = permissionDao.findByRoleId2(role.getId());
            role.setPermissions(permissions);
        }
        // 封装完成,返回
        return user;
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUserName(username);
    }
}
